package p10.code;


import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class PreLoader {

    //创建并初始化一个FutureTask对象
    private final FutureTask<String> future = new FutureTask<>(
            () -> {
                Thread.sleep(10000);
               return  "数据加载完成！！";
            });

    //创建并初始化一个线程
    private final Thread thread = new Thread(future);

    public void start(){
        thread.start();//开始线程
    }

    public String get() throws InterruptedException, ExecutionException {
        try{
            //得到线程加载后计算的结果
            return future.get();
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        PreLoader preLoader = new PreLoader();
        preLoader.start();
        System.out.println(preLoader.get());
    }

}
